﻿using System;
using System.Globalization;
using System.Web;
using System.Web.UI;

namespace Orbitone.MeetingRooms.RoomFinder.UserControls
{
    public partial class DateBrowserUserControl : UserControl
    {
        private DateTime _selectedDate;

        public DateTime SelectedDate
        {
            get { return _selectedDate; }
            set
            {
                _selectedDate = value;
                SetDateLabel(value);
            }
        }

        private void SetDateLabel(DateTime selectedDate)
        {
            lblSelectedDate.Text = FormatDateTime(selectedDate, "en-US", "dddd dd/MM/yyyy");
            btnPrev.NavigateUrl = VirtualPathUtility.ToAbsolute("~/") + "Calendar.aspx?Date=" +
                                  selectedDate.AddDays(-1).ToString("ddMMyyyy");
            btnNext.NavigateUrl = VirtualPathUtility.ToAbsolute("~/") + "Calendar.aspx?Date=" +
                                  selectedDate.AddDays(1).ToString("ddMMyyyy");
        }

        public string FormatDateTime(DateTime dateTime, string lcid, string formatString)
        {
            try
            {
                if (formatString.Length != 0)
                {
                    var provider = new CultureInfo(lcid);
                    return dateTime.ToString(formatString, provider);
                }
                return string.Empty;
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }
    }
}